package sjtu.ChatClient;

import java.util.HashMap;
import java.util.LinkedList;

import net.DataSender;
import android.os.Handler;
/*
 * Manages the 'state' of the client side
 */
public class StateManager {
	private Handler currentHandler;
	private DataSender sender;
	private String [] friends;
	private String localUserName;
	
	private static StateManager manager = null;
	private HashMap<String, String> map = null;
	
	// singleton
	private StateManager(){
		map = new HashMap<String, String>();
		currentHandler = null;
		sender = null;
		friends = new String[0];
		localUserName = "";
	}

	public String getUserIP(String name){
		return map.get(name);
	}
	
	public static StateManager getManager(){
		if(manager == null){
			manager = new StateManager();
		}
		
		return manager;
	}
	
	public void setLocalUserName(String name){
		localUserName = name;
	}
	
	public String getLocalUserName(){
		return localUserName;
	}
	
	/*
	 * should be called in each activity's onResume function
	 */
	public void setCurrentHandler(Handler handler){
		currentHandler = handler;
	}
	
	public Handler getCurrantHandler(){
		return currentHandler;
	}
	
	public void setDataSender(DataSender s){
		sender = s;
	}
	
	public DataSender getDataSender(){
		return sender;
	}
	
	public void setFriends(String [] f){
		friends = f;
	}
	
	public String [] getFriends(){
		return friends;
	}
	
	public void updateFriends(LinkedList<User> users){
		map = new HashMap<String, String>();
		friends = new String[users.size()];
		int i = 0;
		for(User user : users){
			map.put(user.getUserName(), user.getIP());
			friends[i] = user.getUserName();
			++i;
		}
	}
}
